from exchangelib import Account,Credentials,FileAttachment
# credentials = ServiceAccount(username='exchange\wangbin', password='Pass!234')
# account = Account('邮箱', credentials=credentials, autodiscover=True)


credentials = Credentials(username='wangbin@exchange.local', password='Pass!234')
account = Account('wangbin@172.16.14.201', credentials=credentials, autodiscover=True)
print('1.邮箱连接成功')
for item in account.inbox.children:
    print('2.文件夹名称:'+item.name)
    # if item.name=='Reports':#只要Reports文件夹下的附件
    #     index=0
    #     totalcount=0
    #     page=0
    #     while True:
    #         for model in item.all()[page:page+50]:
    #             index=index+1
    #             print(str(index)+'-开始:'+model.subject)
    #             for attachment in model.attachments:y

    #                 if isinstance(attachment, FileAttachment):
    #                     with open(attachment.name, 'wb') as f:
    #                         f.write(attachment.content)
    #         if totalcount==index:
    #             break
    #         page=page+50
    #         totalcount=index

